package com.piece.flowable.controller;

import com.piece.core.framework.annotation.permission.AclPermission;
import com.piece.core.framework.annotation.permission.SubmitPermission;
import com.piece.core.framework.constant.ExceptionConstants;
import com.piece.core.framework.support.bootstrap.DataTable;
import com.piece.core.framework.support.bootstrap.QueryTable;
import com.piece.core.framework.support.convert.Convert;
import com.piece.core.framework.support.response.AjaxResponse;
import com.piece.core.framework.util.basic.I18nUtil;
import com.piece.core.framework.util.string.StringUtil;
import com.piece.core.web.controller.FrameWorkController;
import com.piece.module.flowable.enums.StateType;
import com.piece.module.flowable.service.WfDefinitionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@Api(tags = {"流程定义管理"})
@RequestMapping("/api/workflow/definition")
public class WfDefinitionController extends FrameWorkController {

    @Resource
    private WfDefinitionService wfDefinitionService;

    @PostMapping("/table")
    @AclPermission(code = "flowable:definition:view")
    @ApiOperation(value = "查询流程定义分页数据")
    @ApiImplicitParam(value = "查询条件", dataTypeClass = QueryTable.class, type = "body")
    public AjaxResponse table() {
        QueryTable queryTable = initTable();
        DataTable dataTable = wfDefinitionService.findDefinitionPage(queryTable, null);
        return success(dataTable);
    }

    @PostMapping("/publish/table")
    @AclPermission(code = "flowable:definition:view")
    @ApiOperation(value = "查询流程部署版本分页数据")
    @ApiImplicitParam(value = "查询条件", dataTypeClass = QueryTable.class, type = "body")
    public AjaxResponse publishTable() {
        QueryTable queryTable = initTable();
        String processKey = getRequest().getParameter("processKey");
        DataTable dataTable = wfDefinitionService.findDefinitionPage(queryTable, processKey);
        return success(dataTable);
    }

    @GetMapping("/xml/{definitionId}")
    @AclPermission(code = "flowable:definition:view")
    @ApiOperation(value = "查看流程定义xml文件")
    @ApiImplicitParam(name="definitionId", value = "流程定义ID", type = "path")
    public AjaxResponse readXml(@PathVariable(value = "definitionId") String definitionId) {
        try {
            return success(wfDefinitionService.getDefinitionXml(definitionId));
        } catch (Exception e) {
            return error(I18nUtil.message(ExceptionConstants.NO_RESULT), e.getMessage());
        }
    }

    @GetMapping("/changeState")
    @AclPermission(code = "flowable:definition:state")
    @ApiOperation(value = "激活或挂起流程")
    @ApiImplicitParams({
            @ApiImplicitParam(name="definitionId", value = "流程定义ID"),
            @ApiImplicitParam(name="state", value = "状态", dataTypeClass = StateType.class)
    })
    public AjaxResponse changeState() {
        String definitionId = getRequest().getParameter("definitionId");
        String state = getRequest().getParameter("state");
        try {
            wfDefinitionService.changeDefinitionState(StateType.valueOf(state.toUpperCase()), definitionId);
            return success();
        } catch (Exception e) {
            return error(I18nUtil.message(ExceptionConstants.MESSAGE_ERROR), e.getMessage());
        }
    }

    @SubmitPermission
    @PostMapping("/delete")
    @AclPermission(code = "flowable:definition:delete")
    @ApiOperation(value = "删除流程流程")
    @ApiImplicitParam(name = "ids", value="主键集合")
    public AjaxResponse delete() {
        String ids = getRequest().getParameter("ids");
        if (StringUtil.isNotEmpty(ids)) {
            wfDefinitionService.deleteDefinition(Convert.toStrArray(ids));
        }
        return success(I18nUtil.message(ExceptionConstants.DELETE_SUCCESS), null);
    }
}
